package org.example.springboot01.controller;


import org.example.springboot01.pojo.Result;
import org.example.springboot01.pojo.User;
import org.example.springboot01.service.Userservice;
import org.example.springboot01.utils.JwtUtils;
import org.example.springboot01.utils.MD5Util;
import org.example.springboot01.utils.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class Usercontroller {
    @Autowired
    private Userservice userservice;
    //注册模块--------------
    @RequestMapping("/resign")
    public Result resign(String username, String userpassword) {
            //判断非空
            User user = userservice.Findbyusername(username);
            if (user == null) {
                userservice.adduser(username, userpassword);
                return Result.success("注册成功");
            } else {
                return Result.error("用户重复");
            }
        }
    //登录模块--------------------
    @RequestMapping("/login")
    public Result userlogin(String username,String userpassword){
        boolean b = userservice.login(username);
        if (b==true){
            //登录成了
            //判断密码是否正确
            User user = userservice.Findbyusername(username);
            if(user.getUserpassword().equals(MD5Util.getMD5(userpassword))){
                Map<String,Object> claim = new HashMap<>();
                claim.put("username",user.getUsername());
                String token = JwtUtils.getToken(claim);
                return Result.success(token);
            }else {
                return Result.error("密码错误");
            }
        }
        else{
            return Result.error("登录失败");
        }
    }
    //查询用户信息模块------------------
    @GetMapping("/userinfo")
    public Result<User> getuser(@RequestHeader(name = "Authorization") String token){
        //根据token用户名找用户信息
//        Map<String,Object> claim = JwtUtils.parseToken(token);
//        String username =(String)claim.get("username");
        Map<String,Object> map = ThreadLocalUtil.get();
        String username =(String) map.get("username");
        User user = userservice.Findbyusername(username);
        return Result.success(user);
    }
    //更新用户信息模块---------------
    @PutMapping("/update")
    public Result updateuser(@RequestBody @Validated User user){
        userservice.updateuser(user);
        return Result.success("更新成功");
    }
    //更新用户头像地址-----------------
    @PatchMapping("/updateavatar")
    public Result updateavatar(@RequestParam @URL String avatar){
        //获取用户信息插入图片地址
        userservice.updateavatar(avatar);
        return Result.success("上传用户头像成功");
    }
    //更新用户密码-------------------
    @PatchMapping("/updatepassword")
    public Result updatepassword(@RequestBody Map<String,String> pwd) {
        //校验参数
        String opwd = pwd.get("opwd");
        String npwd = pwd.get("npwd");
        String rpwd = pwd.get("rpwd");
        if(!StringUtils.hasLength(opwd) ||!StringUtils.hasLength(npwd) || !StringUtils.hasLength(rpwd)){
            return Result.error("密码传入错误");
        }
        //调用service层完成密码修改 调用service根据用户名拿到原密码
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        //拿到原密码
        String oldpassword = userservice.Findbyusername(username).getUserpassword();
        //如果原名密码相等才能继续
        if(!MD5Util.getMD5(opwd).equals(oldpassword)){
            return Result.error("原密码错误");
        }
        //新密码和确认密码是否相等
        if(!npwd.equals(rpwd)){
            return Result.error("两次密码不一致");
        }
        //调用service完成密码修改
        userservice.updatepassword(npwd);
        return Result.success("修改成功");
    }

}



